home *** CD-ROM | disk | FTP | other *** search
/ Aminet 16 / Aminet 16 (1996)(GTI - Schatztruhe)[!][Dec 1996].iso / Aminet / comm / term / term_source.lha / Extras / Source / term-source.lha / Raster.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  9KB  |  508 lines

  1. /*
  2. **    Raster.c
  3. **
  4. **    Screen character (raster) buffering routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* DeleteRaster():
  17.      *
  18.      *    Free the contents of the character raster.
  19.      */
  20.  
  21. VOID
  22. DeleteRaster()
  23. {
  24.     FreeVecPooled(Raster);
  25.     Raster = NULL;
  26.  
  27.     FreeVecPooled(RasterAttr);
  28.     RasterAttr = NULL;
  29. }
  30.  
  31.     /* CreateRaster():
  32.      *
  33.      *    Create the character raster.
  34.      */
  35.  
  36. BOOL
  37. CreateRaster()
  38. {
  39.         /* Width of the screen * 2 (in characters),
  40.          * extra for double width. The window size
  41.          * may change, the screen size hopefully
  42.          * doesn't.
  43.          */
  44.  
  45.     RasterWidth        = (Window->WScreen->Width / TextFontWidth) * 2;
  46.  
  47.         /* Height of the character raster. */
  48.  
  49.     RasterHeight    = Window->WScreen->Height / TextFontHeight;
  50.  
  51.         /* Allocate the raster. */
  52.  
  53.     if(Raster = (STRPTR)AllocVecPooled(RasterWidth * RasterHeight,MEMF_ANY | MEMF_CLEAR))
  54.     {
  55.             /* Allocate the raster attributes. */
  56.  
  57.         if(RasterAttr = (STRPTR)AllocVecPooled(RasterHeight,MEMF_ANY | MEMF_CLEAR))
  58.             return(TRUE);
  59.     }
  60.  
  61.     DeleteRaster();
  62.  
  63.     return(FALSE);
  64. }
  65.  
  66.     /* RasterEraseScreen(BYTE Mode):
  67.      *
  68.      *    Erase parts of the screen.
  69.      */
  70.  
  71. VOID
  72. RasterEraseScreen(LONG Mode)
  73. {
  74.     LONG First,Last;
  75.  
  76.     ObtainSemaphore(&RasterSemaphore);
  77.  
  78.     switch(Mode)
  79.     {
  80.         case 1:
  81.  
  82.             First    = 0;
  83.             Last    = CursorY * RasterWidth + CursorX + 1;
  84.  
  85.             if(CursorY == LastLine)
  86.                 SaveRaster(0,CursorY);
  87.  
  88.             memset(RasterAttr,SCALE_NORMAL,CursorY + 1);
  89.  
  90.             break;
  91.  
  92.         case 2:
  93.  
  94.             First    = 0;
  95.             Last    = RasterHeight * RasterWidth - 1;
  96.  
  97.             SaveRaster(0,RasterHeight - 1);
  98.  
  99.             memset(RasterAttr,SCALE_NORMAL,RasterHeight);
  100.  
  101.             break;
  102.  
  103.         default:
  104.  
  105.             First    = CursorY * RasterWidth + CursorX;
  106.             Last    = RasterHeight * RasterWidth - 1;
  107.  
  108.             if(CursorY == 0)
  109.                 SaveRaster(CursorY,RasterHeight - 1);
  110.  
  111.             memset(&RasterAttr[CursorY],SCALE_NORMAL,RasterHeight - CursorY);
  112.  
  113.             break;
  114.     }
  115.  
  116.     RethinkRasterLimit();
  117.  
  118.     if(Last > First)
  119.         memset(&Raster[First],' ',Last - First);
  120.  
  121.     ConFontScaleUpdate();
  122.  
  123.     ReleaseSemaphore(&RasterSemaphore);
  124. }
  125.  
  126.     /* RasterEraseLine(BYTE Mode):
  127.      *
  128.      *    Erase parts of the current cursor line.
  129.      */
  130.  
  131. VOID
  132. RasterEraseLine(LONG Mode)
  133. {
  134.     LONG First,Last;
  135.  
  136.     ObtainSemaphore(&RasterSemaphore);
  137.  
  138. /*    SaveRaster(CursorY,CursorY); */
  139.  
  140.     switch(Mode)
  141.     {
  142.             /* From beginning to current cursor position. */
  143.  
  144.         case 1:
  145.  
  146.             First    = CursorY * RasterWidth;
  147.             Last    = First + CursorX + 1;
  148.  
  149.             break;
  150.  
  151.             /* Entire line. */
  152.  
  153.         case 2:
  154.  
  155.             First    = CursorY * RasterWidth;
  156.             Last    = First + RasterWidth - 1;
  157.  
  158.             break;
  159.  
  160.             /* From current cursor position towards end. */
  161.  
  162.         default:
  163.  
  164.             First    = CursorY * RasterWidth + CursorX;
  165.             Last    = (CursorY + 1) * RasterWidth - 1;
  166.  
  167.             break;
  168.     }
  169.  
  170.     if(Last > First)
  171.         memset(&Raster[First],' ',Last - First);
  172.  
  173.     ReleaseSemaphore(&RasterSemaphore);
  174. }
  175.  
  176.     /* RasterEraseCharacters(LONG Chars):
  177.      *
  178.      *    Erase a number of characters in the current cursor
  179.      *    line.
  180.      */
  181.  
  182. VOID
  183. RasterEraseCharacters(LONG Chars)
  184. {
  185.     if(CursorX < RasterWidth - 1)
  186.     {
  187.         LONG First,Diff;
  188.         UBYTE *To,*From;
  189.  
  190. /*        SaveRaster(CursorY,CursorY); */
  191.  
  192.         ObtainSemaphore(&RasterSemaphore);
  193.  
  194.         First    = CursorY * RasterWidth + CursorX;
  195.         To        = &Raster[First];
  196.  
  197.         if(CursorX + Chars >= RasterWidth)
  198.         {
  199.             Diff = RasterWidth - 1 - CursorX;
  200.  
  201.             while(Diff-- > 0)
  202.                 *To++ = ' ';
  203.         }
  204.         else
  205.         {
  206.             From = &Raster[First + Chars];
  207.             Diff = RasterWidth - (CursorX + 1 + Chars);
  208.  
  209.             while(Diff--)
  210.             {
  211.                 *To++ = *From;
  212.  
  213.                 *From++ = ' ';
  214.             }
  215.         }
  216.  
  217.         ReleaseSemaphore(&RasterSemaphore);
  218.     }
  219. }
  220.  
  221.     /* RasterClearLine(LONG Lines):
  222.      *
  223.      *    Clear and remove a number of lines.
  224.      */
  225.  
  226. VOID
  227. RasterClearLine(LONG Lines,LONG Top)
  228. {
  229.     if(Lines)
  230.     {
  231.         LONG RegionBottom;
  232.  
  233.         ObtainSemaphore(&RasterSemaphore);
  234.  
  235.         if(RegionSet)
  236.             RegionBottom = Bottom;
  237.         else
  238.             RegionBottom = LastLine + 1;
  239.  
  240.         if(Top + Lines >= RegionBottom + 1)
  241.         {
  242.             SaveRaster(Top,RegionBottom);
  243.  
  244.             Lines = RegionBottom - Top + 1;
  245.  
  246.             memset(&Raster[Top * RasterWidth],' ',RasterWidth * Lines);
  247.             memset(&RasterAttr[Top],SCALE_NORMAL,Lines);
  248.         }
  249.         else
  250.         {
  251.             UBYTE *From,*To;
  252.             LONG Max;
  253.  
  254.             SaveRaster(Top,Top + Lines - 1);
  255.  
  256.             Max    = (RegionBottom - (Top + Lines)) * RasterWidth;
  257.  
  258.             From    = &Raster[(Top + Lines) * RasterWidth];
  259.             To        = &Raster[ Top          * RasterWidth];
  260.  
  261.             while(Max--)
  262.             {
  263.                 *To++ = *From;
  264.  
  265.                 *From++ = ' ';
  266.             }
  267.  
  268.             memset(&RasterAttr[RegionBottom - Lines],SCALE_NORMAL,Lines);
  269.  
  270.         }
  271.  
  272.         RethinkRasterLimit();
  273.  
  274.         ConFontScaleUpdate();
  275.  
  276.         ReleaseSemaphore(&RasterSemaphore);
  277.     }
  278.  
  279. }
  280.  
  281.     /* RasterInsertLine(LONG Lines):
  282.      *
  283.      *    Insert a number of lines at the current cursor line.
  284.      */
  285.  
  286. VOID
  287. RasterInsertLine(LONG Lines,LONG Top)
  288. {
  289.     if(Lines)
  290.     {
  291.         LONG RegionBottom;
  292.  
  293.         if(RegionSet)
  294.             RegionBottom = Bottom;
  295.         else
  296.             RegionBottom = LastLine + 1;
  297.  
  298.         if(Top + Lines >= RegionBottom + 1)
  299.         {
  300.             SaveRaster(Top,RegionBottom);
  301.  
  302.             Lines = RegionBottom - Top + 1;
  303.  
  304.             memset(&Raster[Top * RasterWidth],' ',RasterWidth * Lines);
  305.             memset(&RasterAttr[Top],SCALE_NORMAL,Lines);
  306.         }
  307.         else
  308.         {
  309.             UBYTE *FromPtr,*ToPtr;
  310.             LONG From,To,Max;
  311.  
  312.             SaveRaster(RegionBottom - Lines,RegionBottom);
  313.  
  314.             ObtainSemaphore(&RasterSemaphore);
  315.  
  316.             Max    = (RegionBottom - Lines - Top) * RasterWidth;
  317.  
  318.             From    = (RegionBottom - Lines) * RasterWidth - 1;
  319.             To        =  RegionBottom          * RasterWidth - 1;
  320.  
  321.             FromPtr    = &Raster[From];
  322.             ToPtr    = &Raster[To];
  323.  
  324.             while(Max--)
  325.                 *ToPtr-- = *FromPtr--;
  326.  
  327.             memset(&Raster[Top * RasterWidth],' ',Lines * RasterWidth);
  328.  
  329.             ReleaseSemaphore(&RasterSemaphore);
  330.         }
  331.     }
  332.  
  333. }
  334.  
  335.     /* RasterScrollRegion(LONG Direction,LONG RasterTop,LONG RasterBottom,LONG RasterLines):
  336.      *
  337.      *    Scroll the contents of the character raster up/down.
  338.      */
  339.  
  340. VOID
  341. RasterScrollRegion(LONG Direction,LONG RasterTop,LONG RasterBottom,LONG RasterLines)
  342. {
  343.     LONG Dir = ABS(Direction);
  344.  
  345.     ObtainSemaphore(&RasterSemaphore);
  346.  
  347.     SaveRaster(RasterTop,RasterTop + RasterLines - 1);
  348.  
  349.     if(Dir >= RasterLines)
  350.     {
  351.             /* All that is needed is to delete the lines. */
  352.  
  353.         memset(&Raster[RasterTop * RasterWidth],' ',RasterLines * RasterWidth);
  354.     }
  355.     else
  356.     {
  357.         LONG First,Last,Max,i;
  358.         UBYTE *From,*To;
  359.  
  360.         Max = (RasterLines - Dir) * RasterWidth;
  361.  
  362.         if(Direction < 0)
  363.         {
  364.             First    = (RasterTop + RasterLines - Dir) * RasterWidth - 1;
  365.             Last    = (RasterTop + RasterLines    ) * RasterWidth - 1;
  366.  
  367.             From    = &Raster[First];
  368.             To        = &Raster[Last];
  369.  
  370.             while(Max--)
  371.                 *To-- = *From--;
  372.  
  373.             for(i = RasterBottom ; i >= (RasterTop + Dir) ; i--)
  374.                 RasterAttr[i] = RasterAttr[i - Dir];
  375.  
  376.             memset(&Raster[RasterTop * RasterWidth],' ',RasterWidth * Dir);
  377.  
  378.             memset(&RasterAttr[RasterTop],SCALE_NORMAL,Dir);
  379.         }
  380.         else
  381.         {
  382.             First    = RasterTop * RasterWidth + RasterWidth * Dir;
  383.             Last    = RasterTop * RasterWidth;
  384.  
  385.             From    = &Raster[First];
  386.             To        = &Raster[Last];
  387.  
  388.             while(Max--)
  389.                 *To++ = *From++;
  390.  
  391.             memset(&Raster[(RasterBottom - Dir) * RasterWidth],' ',RasterWidth * Dir);
  392.  
  393.             for(i = RasterTop ; i <= (RasterBottom - Dir) ; i++)
  394.                 RasterAttr[i] = RasterAttr[i + Dir];
  395.  
  396.             memset(&RasterAttr[RasterBottom - Dir],SCALE_NORMAL,Dir);
  397.         }
  398.     }
  399.  
  400.     RethinkRasterLimit();
  401.  
  402.     ConFontScaleUpdate();
  403.  
  404.     ReleaseSemaphore(&RasterSemaphore);
  405. }
  406.  
  407.     /* RasterShiftChar(LONG Size):
  408.      *
  409.      *    Shift the characters following the current cursor
  410.      *    position Size characters to the right.
  411.      */
  412.  
  413. VOID
  414. RasterShiftChar(LONG Size)
  415. {
  416.     UBYTE *From,*To;
  417.     LONG First,i;
  418.  
  419.     ObtainSemaphore(&RasterSemaphore);
  420.  
  421.     if(CursorX + Size >= RasterWidth - 1)
  422.     {
  423.         i    = RasterWidth - 1 - CursorX;
  424.         To    = &Raster[CursorY * RasterWidth + CursorX];
  425.  
  426.         while(i-- > 0)
  427.             *To++ = ' ';
  428.     }
  429.     else
  430.     {
  431.         First    = (CursorY + 1) * RasterWidth - 1;
  432.         To        = &Raster[First];
  433.  
  434.         From    = &Raster[First - Size];
  435.         i        = RasterWidth - Size;
  436.  
  437.         while(i-- > CursorX)
  438.             *To-- = *From--;
  439.  
  440.         To        = &Raster[CursorY * RasterWidth + CursorX];
  441.  
  442.         while(Size--)
  443.             *To++ = ' ';
  444.     }
  445.  
  446.     ReleaseSemaphore(&RasterSemaphore);
  447. }
  448.  
  449.     /* RasterPutString(STRPTR String,LONG Length):
  450.      *
  451.      *    Put a string into the character raster.
  452.      */
  453.  
  454. VOID
  455. RasterPutString(STRPTR String,LONG Length)
  456. {
  457.     ObtainSemaphore(&RasterSemaphore);
  458.  
  459.     if(Length == 1)
  460.     {
  461.         if(CursorX + 1 < RasterWidth)
  462.             Raster[CursorY * RasterWidth + CursorX] = String[0];
  463.     }
  464.     else
  465.     {
  466.         if(CursorX + Length >= RasterWidth)
  467.             Length = RasterWidth - 1 - CursorX;
  468.  
  469.         if(Length > 0)
  470.             CopyMem(String,&Raster[CursorY * RasterWidth + CursorX],Length);
  471.     }
  472.  
  473.     ReleaseSemaphore(&RasterSemaphore);
  474. }
  475.  
  476. VOID
  477. SaveRasterDummy(LONG UnusedFirst,LONG UnusedLast)
  478. {
  479. }
  480.  
  481. VOID
  482. SaveRasterReal(LONG First,LONG Last)
  483. {
  484.     STRPTR Line,This;
  485.     LONG Size,i;
  486.  
  487.     ObtainSemaphore(&RasterSemaphore);
  488.  
  489.     if(First < 0)
  490.         First = 0;
  491.  
  492.     if(Last > RasterHeight - 1)
  493.         Last = RasterHeight - 1;
  494.  
  495.     for(i = First, This = &Raster[(LONG)First * RasterWidth] ; i <= Last ; i++, This += RasterWidth)
  496.     {
  497.         Line = This;
  498.         Size = RasterWidth - 1;
  499.  
  500.         while(Size > 0 && Line[Size - 1] == ' ')
  501.             Size--;
  502.  
  503.         AddLine(Line,Size);
  504.     }
  505.  
  506.     ReleaseSemaphore(&RasterSemaphore);
  507. }
  508.